package com.example.imooccommon.store;

import java.util.Arrays;

import reactor.core.support.Assert;

/**
 * @Author Anita
 * @Date 2018/1/18 11:07
 */
public class FileTypeHelper {

    public static FileType getType(byte[] buff){
        Assert.isTrue(buff!=null&&buff.length>28);
        byte[] bytes = Arrays.copyOfRange(buff,0,28 );
        String magic = bytesToHex(bytes);
        for(FileType type:FileType.values()){
            if(magic.startsWith(type.getValue()))
                return type;
        }

        return null;
    }

    private static String bytesToHex(byte[] src){
        StringBuilder stringBuilder = new StringBuilder();
        if(src == null||src.length<=0) return null;
        for(int i=0;i<src.length;i++){
            int v = src[i]&0xFF;
            String hv = Integer.toHexString(v);
            if(hv.length()<2){
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString().toUpperCase();
    }
}
